﻿Ext.define('Admin.view.leaveapprove.LeaveApproveViewController', {
    extend: Ext.app.ViewController,
    alias: 'controller.leaveapprove',
    onClickLeaveApproveClaimButton: function(view, recIndex, cellIndex, item, e, record) {
        Ext.Ajax.request({
            url: 'leave/claim/' + record.get('taskId'),
            method: 'post',
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('操作成功', json.msg, function() {
                        view.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('操作失败', json.msg);
                }
            }
        });
    },
    setCurrentView: function(view, title) {
        if (view) {
            var cfg = Ext.apply({
                    xtype: 'leaveapprove-window',
                    items: [
                        Ext.apply({
                            xtype: view
                        }, '')
                    ]
                }, {
                    title: title
                });
            Ext.create(cfg);
        }
    },
    	
    	
    onClickLeaveApproveCompleteWindowButton: function(view, recIndex, cellIndex, item, e, record) {
    	//选中点击的行
        var taskDefinitionKey = record.get('taskDefinitionKey');
        if (taskDefinitionKey == 'deptLeaderAudit') {
            //部门领导审批
            this.setCurrentView('deptleaderAudit_form', '部门领导审批');
        } else if (taskDefinitionKey == 'hrAudit') {
        	//人事审批
        	//this.setCurrentView('人事审批表单', {record: record});
        }
        else if (taskDefinitionKey == 'reportBack') {
        	 //申请人销假
        	//this.setCurrentView('销假表单', {record: record});
        }
        else if (taskDefinitionKey == 'modifyApply') {
        	//申请人调整申请：可以编写到工具类中
        	//this.setCurrentView('调整申请表单', {record: record});
        }
    },
    
	complete: function(url, variables ,grid ,form){
		// 转换JSON为字符串
	    var keys = "", values = "", types = "";
		if (variables) {
			Ext.each(variables, function (item) {
				if (keys != "") {
					keys += ",";
					values += ",";
					types += ",";
				}
				keys += item.key;
				values += item.value;
				types += item.type;
            });
		}
		Ext.Ajax.request({
            url: url,
            method: 'post',
            params : { 
			 	keys: keys,
		        values: values,
		        types: types
			}, 
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('操作成功', json.msg, function() {
                    	form.up('window').close();
                        grid.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('操作失败', json.msg);
                }
            }
        });
	},
    	
    onClickDeptleaderAuditFormSubmitButton: function(btn) {
        var gridPanel = Ext.getCmp('leaveapprove-grid');
        //提示必须选中一行记录
        //var selection = gridPanel.getSelectionModel().getSelection();
        //if(selection.length==0) {
	    //  	Ext.MessageBox.alert('提示','请选中一条审批记录，进行审批！');
	    //}else {
	    	var form = btn.up('form');
	    	var record = gridPanel.getSelectionModel().getLastSelected();
	    	var url = 'leave/complete/' + record.get('taskId');
	    	var variables = [{
				key: 'deptLeaderPass',
				value: true,//获取表单选择的value
				type: 'B'
			}];
	        this.complete(url,variables,gridPanel,form);
		//}
       
        
    },
    onClickLeaveApproveGridAddButton: function(btn) {
        alert("on Click Add Button!");
    },
    onClickAddFormSubmitButton: function(btn) {
        alert("on Click Submit Button!");
    },
    onClickLeaveApproveGridUpdateButton: function(btn) {
        alert("on Click Update Button!");
    },
    onClickLeaveApproveGridDeleteButton: function(btn) {
        alert("on Click Delete Button!");
    }
});